package utils.utilTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilTime {
    private static final SimpleDateFormat simpleDateFormat;

    static {
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static String getUnixTimeSNow() {
        return String.valueOf(System.currentTimeMillis());
    }

    public static Long getUnixTimeLNow() {
        return System.currentTimeMillis();
    }

    public static String getStringTime(Long unixTime) {
        Date date = new Date(unixTime);
        return simpleDateFormat.format(date);
    }

    public static String getStringTimeNow(){
        return simpleDateFormat.format(new Date(System.currentTimeMillis()));
    }

    public static Long getUnixTime(String stringTime) throws ParseException {
        Date date = simpleDateFormat.parse(stringTime);
        return date.getTime();
    }

    public static Long plusUnixTime(String unixTime, int index, String unit) throws Exception {
        return plusUnixTime(Long.valueOf(unixTime), index, unit);
    }

    public static Long plusUnixTime(Long unixTime, int index, String unit) throws Exception {
        switch (unit) {
            case "ms": {
                return unixTime + index;
            }
            case "s": {
                return unixTime + index * 1000L;
            }
            case "m": {
                return unixTime + index * 60000L;
            }
            case "h": {
                return unixTime + index * 3600000L;
            }
            case "D": {
                return unixTime + index * 86400000L;
            }
            case "W": {
                return unixTime + index * 604800000L;
            }
            default:
                throw new Exception("[Execute] Input Parameter Error : Valid parameters are as follows ms/s/m/h/D/W/M ");
        }
    }

    public static Long minusUnixTime(String unixTime, int index, String unit) throws Exception {
        return plusUnixTime(Long.valueOf(unixTime), index, unit);
    }

    public static Long minusUnixTime(Long unixTime, int index, String unit) throws Exception {
        switch (unit) {
            case "ms": {
                unixTime -= index;
            }
            break;
            case "s": {
                unixTime -= index * 1000L;
            }
            break;
            case "m": {
                unixTime -= index * 60000L;
            }
            break;
            case "h": {
                unixTime -= index * 3600000L;
            }
            break;
            case "D": {
                unixTime -= index * 86400000L;
            }
            break;
            case "W": {
                unixTime -= index * 604800000L;
            }
            break;
            default:
                throw new Exception("[INPUT] Input Parameter Error : Valid parameters are as follows ms/s/m/h/D/W/M ");
        }
        if(unixTime>=0)return unixTime;
        else throw new Exception("EXECUTE Input data Error : UnixTime can't be less than 0");
    }
}
